package org.jmh.gift.util;

import lombok.Data;
import org.jmh.gift.enums.SystemErrorCode;
import org.jmh.gift.exception.ResponseCode;

@Data
public class AjaxResult {
    private Integer code = ResponseCode.RESPONSE_CODE_200.getCode();     // 默认200，表示操作成功
    private Boolean success = true;    // 表示当前操作是成功还是失败 true:成功 false:失败
    private String message = ResponseCode.RESPONSE_CODE_200.getMessage();     // 失败的原因
    private Object resultObj;   // 返回给前端的查询结果对象,是一定不能把类型写死的,因为这个AjaxResult类是公共的,每个controller都是返回AjaxResult类的
    private String msg;
    private Object data;

    public AjaxResult(){
        this.success = true;
    }

    public static AjaxResult success(){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.msg = "操作成功";
        ajaxResult.success = true;
        return ajaxResult;
    }
    public static AjaxResult success(Object obj){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.msg = "成功";
        ajaxResult.success = true;
        ajaxResult.data = obj;
        return ajaxResult;
    }

    public static AjaxResult error(){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.msg = "失败";
        ajaxResult.success = false;
        return ajaxResult;
    }
    public static AjaxResult error(String msg){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.msg = msg;
        ajaxResult.success = false;
        return ajaxResult;
    }
    public static AjaxResult error(SystemErrorCode systemErrorCode){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.msg = systemErrorCode.getMsg();
        ajaxResult.code= systemErrorCode.getCode();
        ajaxResult.success = false;
        return ajaxResult;
    }
    public Integer getCode() {
        return code;
    }

    public AjaxResult setCode(Integer code) {
        this.code = code;
        return this;
    }
    public AjaxResult setSuccess(Boolean success) {
        this.success = success;
        return this;    // this它的意思是:谁调用我我就是谁
    }
    public String getMessage() {
        return message;
    }

    public AjaxResult setMessage(String message) {
        this.message = message;
        return this;
    }
    public static AjaxResult me(){
        return new AjaxResult();
    }
    public Object getResultObj() {
        return resultObj;
    }

    public AjaxResult setResultObj(Object resultObj) {
        this.resultObj = resultObj;
        return this;
    }
}
